"""
待办事项Pydantic模式

功能说明：
1. 定义待办事项的请求和响应模式
2. 提供数据验证和序列化
3. 支持多状态管理
"""

from pydantic import BaseModel, Field, validator
from typing import Optional, List
from datetime import datetime
from enum import Enum


class TodoStatus(str, Enum):
    """待办事项状态枚举"""
    PENDING = "pending"          # 待完成
    IN_PROGRESS = "in_progress"  # 进行中
    COMPLETED = "completed"      # 已完成


class TodoBase(BaseModel):
    """待办事项基础模式"""
    title: str = Field(..., min_length=1, max_length=50, description="待办事项标题")
    content: Optional[str] = Field(None, max_length=500, description="待办事项内容")
    status: TodoStatus = Field(TodoStatus.PENDING, description="待办事项状态")
    
    @validator('title')
    def validate_title(cls, v):
        if not v or not v.strip():
            raise ValueError('标题不能为空')
        return v.strip()
    
    @validator('content')
    def validate_content(cls, v):
        if v is not None:
            return v.strip() if v.strip() else None
        return v


class TodoCreate(TodoBase):
    """创建待办事项请求模式"""
    pass


class TodoUpdate(BaseModel):
    """更新待办事项请求模式"""
    title: Optional[str] = Field(None, min_length=1, max_length=50, description="待办事项标题")
    content: Optional[str] = Field(None, max_length=500, description="待办事项内容")
    status: Optional[TodoStatus] = Field(None, description="待办事项状态")
    
    @validator('title')
    def validate_title(cls, v):
        if v is not None and (not v or not v.strip()):
            raise ValueError('标题不能为空')
        return v.strip() if v else v
    
    @validator('content')
    def validate_content(cls, v):
        if v is not None:
            return v.strip() if v.strip() else None
        return v


class TodoResponse(TodoBase):
    """待办事项响应模式"""
    id: int = Field(..., description="待办事项ID")
    user_id: int = Field(..., description="用户ID")
    create_time: datetime = Field(..., description="创建时间")
    update_time: Optional[datetime] = Field(None, description="更新时间")
    
    class Config:
        from_attributes = True


class TodoListResponse(BaseModel):
    """待办事项列表响应模式"""
    items: List[TodoResponse] = Field(..., description="待办事项列表")
    total: int = Field(..., description="总数量")
    skip: int = Field(..., description="跳过数量")
    limit: int = Field(..., description="限制数量")


class TodoStatsResponse(BaseModel):
    """待办事项统计响应模式"""
    total: int = Field(..., description="总数量")
    pending: int = Field(..., description="待完成数量")
    in_progress: int = Field(..., description="进行中数量")
    completed: int = Field(..., description="已完成数量")


class TodoBatchDeleteRequest(BaseModel):
    """批量删除待办事项请求模式"""
    todo_ids: List[int] = Field(..., min_items=1, description="待办事项ID列表")
    
    @validator('todo_ids')
    def validate_todo_ids(cls, v):
        if not v:
            raise ValueError('待办事项ID列表不能为空')
        if len(set(v)) != len(v):
            raise ValueError('待办事项ID列表不能包含重复项')
        return v


class TodoQueryParams(BaseModel):
    """待办事项查询参数模式"""
    status: Optional[TodoStatus] = Field(None, description="状态筛选")
    skip: int = Field(0, ge=0, description="跳过数量")
    limit: int = Field(20, ge=1, le=100, description="限制数量")
    
    class Config:
        from_attributes = True
